#include <k_api.h>
#include <aos/kernel.h>
#include "para_traverse_task.h"
#include "img_cli_deal.h"
#include "sfh4775s.h"
#include "expo_para.h"

extern int aos_task_delete(char *name);

aos_sem_t para_traverse_sem;
aos_sem_t para_traverse_ready_sem;
aos_sem_t para_traverse_complete_sem;

traverse_para_t traverse_para = { 
    .ir     =           {IR_CURRENT_REG, 0x0},      //ir
    .spk    =           {SPK_CURRENT_REG, 0x0},     //spk
    .ov9282 =           {   
                            {0x3501, 0x0},	        //expo H		
                            {0x3502, 0x0},          //expo L
                            {0x3509, 0x0},          //gain
                        },   
    .ov9282_strobe =    {   
                            {0x3927, 0x0},	        // strobe width		
                            {0x3928, 0x0},          // 
                            {0x3929, 0x0},          // strobe start time
                            {0x392A, 0x0},          //
                        },
}; 


void print_traverse_para(void)
{
    printf("ir = 0x%x\n", traverse_para.ir.data);
    printf("spk = 0x%x\n", traverse_para.spk.data);
    printf("expH = 0x%x\n", traverse_para.ov9282[0].data);
    printf("expL = 0x%x\n", traverse_para.ov9282[1].data);
    printf("gain = 0x%x\n", traverse_para.ov9282[2].data);
#if defined(LED_STROBE)	    
    printf("strobe[0] = 0x%x\n", traverse_para.ov9282_strobe[0].data);
    printf("strobe[1] = 0x%x\n", traverse_para.ov9282_strobe[1].data);
    printf("strobe[2] = 0x%x\n", traverse_para.ov9282_strobe[2].data);
    printf("strobe[3] = 0x%x\n", traverse_para.ov9282_strobe[3].data);
#endif    
}

static void para_traverse_task(void *arg)
{
    int i = 0, j = 0;

    while (1) {
        aos_sem_wait(&para_traverse_sem, AOS_WAIT_FOREVER);
        for (i = traverse_para.exp_start; i <= traverse_para.exp_end; i++) {
            traverse_para.ov9282[0].data = expo_para_list[i].expo[0];                       /* expoH */
            traverse_para.ov9282[1].data = expo_para_list[i].expo[1];                       /* expoL */
#if defined(LED_STROBE)	            
            traverse_para.ov9282_strobe[0].data = strobe_para_list[i].strobe[0];
            traverse_para.ov9282_strobe[1].data = strobe_para_list[i].strobe[1];
            traverse_para.ov9282_strobe[2].data = strobe_para_list[i].strobe[2];
            traverse_para.ov9282_strobe[3].data = strobe_para_list[i].strobe[3];
#endif            
            for (j = traverse_para.gain_start; j <= traverse_para.gain_end; j++) {
                traverse_para.ov9282[2].data = gain_para_list[j];                           /* gain */
                aos_sem_signal(&para_traverse_ready_sem);
                aos_sem_wait(&para_traverse_sem, AOS_WAIT_FOREVER);
            }
        }
        // printf("para traverse complete.\n");
        aos_sem_signal(&para_traverse_ready_sem);
        aos_sem_signal(&para_traverse_complete_sem);
    }
}

void para_traverse_task_init(void)
{
    aos_sem_new(&para_traverse_sem, 0);
    aos_sem_new(&para_traverse_ready_sem, 0);
    aos_sem_new(&para_traverse_complete_sem, 0);
    aos_task_new(PARA_TRAVERSE_MODULE, para_traverse_task, NULL, 10*1024);
    printf("%s created!\n", PARA_TRAVERSE_MODULE);

    return;
}

void pack_trav_para_list(uint8_t *buf)
{
	*(buf + 0) = traverse_para.ir.data; 
	*(buf + 1) = traverse_para.spk.data; 
	*(buf + 2) = traverse_para.ov9282[0].data; 
	*(buf + 3) = traverse_para.ov9282[1].data; 
	*(buf + 4) = traverse_para.ov9282[2].data; 
}

void set_trav_para_list_invalid()
{
	traverse_para.ir.data = TRAVERSE_END; 
	traverse_para.spk.data = TRAVERSE_END; 
	traverse_para.ov9282[0].data = TRAVERSE_END; 
	traverse_para.ov9282[1].data = TRAVERSE_END; 
	traverse_para.ov9282[2].data = TRAVERSE_END; 
}

void para_traverse_task_destroy(void)
{
    aos_sem_free(&para_traverse_sem);
    aos_sem_free(&para_traverse_ready_sem);
    aos_sem_free(&para_traverse_complete_sem);
    aos_task_delete(PARA_TRAVERSE_MODULE);
    printf("%s delete!\n", PARA_TRAVERSE_MODULE);
}